//
//  LayerEnd.cpp
//  lhq
//
//  Created by codin on 14-7-24.
//
//

#include "LayerEnd.h"

USING_NS_CC;

Scene* LayerEnd::createScene() {
    auto scene = Scene::create();
    auto layer = LayerEnd::create();
    scene->addChild(layer);
    return scene;
}

bool LayerEnd::init(){
    if (!Layer::init()){
        return false;
    }
    
    auto director = Director::getInstance();
    Size visibleSize = director->getVisibleSize();
    Vec2 origin = director->getVisibleOrigin();
    
    auto bgSp = Sprite::create("texture/end/bg.jpg");
    bgSp->setPosition(visibleSize.width/2,visibleSize.height/2);
    this->addChild(bgSp);
    
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/end/end.plist", "texture/end/end.png");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("texture/cards/cards.plist", "texture/cards/cards.png");
    
//    auto isWin = this->endInfo.winLocaction===(Constants.MAX_PLAYER_COUNT-1);
//    //        auto isDianPao = this->endInfo.paoLocaction===(Constants.MAX_PLAYER_COUNT-1);//是否是我点炮
//    auto isDianPao = false;
//    auto winLocationName = this->getLocName(this->endInfo.winLocaction);
//    auto huxi = this->huInfo.huxi;
//    auto winVm = Math.abs(this->endInfo.myGameVm);
//    auto costVm = this->endInfo.myGameCost;
//    auto winScore = this->endInfo.myGameScore;
    
    auto isWin = false;
    auto isDianPao = false;
    auto winLocationName = "shangjia";
    auto huxi = 10;
    auto winVm = 10;
    auto costVm = 20;
    auto winScore = 100;
    
//    // TODO 连胡，大胡等 提示不一样
    auto titleImg = isWin ? "end_title_win.png" : "end_title_lose.png";
    titleImg = isDianPao ? "end_title_lose_pao.png" : titleImg;
    auto titleSp = Sprite::createWithSpriteFrameName(titleImg);
    titleSp->setScale(0.8);
    titleSp->setPosition(visibleSize.width/2,visibleSize.height-100);
    this->addChild(titleSp);

    auto ladySp = Sprite::create(StringUtils::format("texture/lady/lady_%s.png",isWin?"win":"lose"));
    ladySp->setScale(0.8);
    ladySp->setPosition(100,220);
    this->addChild(ladySp);

    // 谁赢了
    auto whoWinSp = Sprite::createWithSpriteFrameName(StringUtils::format("end_whowin_%s.png",winLocationName));
    whoWinSp->setPosition(350,visibleSize.height-200);
    this->addChild(whoWinSp);
    
    // 胡息
    auto huxiLb = LabelTTF::create("10","Helvetica",50);
    huxiLb->setPosition(485,visibleSize.height-200);
    this->addChild(huxiLb);

    // 番 胡息
    auto fanSp = Sprite::createWithSpriteFrameName("end_title_fan.png");
    fanSp->setPosition(550,visibleSize.height-200);
    this->addChild(fanSp);

    // 得/失
    auto deShiSp = Sprite::createWithSpriteFrameName(isWin?"end_title_de.png":"end_title_shi.png");
    deShiSp->setPosition(700,visibleSize.height-200);
    this->addChild(deShiSp);

    // vm
    auto vmLb = LabelTTF::create("100","Helvetica",50);
    vmLb->setPosition(800,visibleSize.height-200);
    this->addChild(vmLb);

    // 豆
    auto douSp = Sprite::createWithSpriteFrameName("end_title_fan_dou.png");
    douSp->setPosition(900,visibleSize.height-200);
    this->addChild(douSp);
    
    // score 积分（送话费） TODO 连胡，大胡 积分不一样，
    if(winScore>0){
        auto scoreLb = LabelTTF::create(StringUtils::format("加 %d 话费点.",winScore),"Helvetica",40);
        scoreLb->setPosition(750,visibleSize.height-120);
        this->addChild(scoreLb);
    }
    
//    // 牌
//    this->addBLocks();
    
    // 继续游戏
    auto btnSp0 = Sprite::createWithSpriteFrameName("end_jixu.png");
    auto btnSp1 = Sprite::createWithSpriteFrameName("end_jixu_sel.png");
    auto continueBtn=MenuItemSprite::create(btnSp0,btnSp1,CC_CALLBACK_1(LayerEnd::continueGame,this));
    continueBtn->setPosition(visibleSize.width/2+50,90);
    auto menu = Menu::create(continueBtn,nullptr);
    menu->setPosition(0,0);
    this->addChild(menu,30);
    
    // cost vm
    auto costLb = LabelTTF::create(StringUtils::format("本局游戏消耗了 %d 豆子",costVm),"Helvetica",30);
    costLb->setPosition(visibleSize.width/2+100,15);
    this->addChild(costLb);
    
    return true;
}

void LayerEnd::continueGame(Ref* sender){
    log("continueGame");
}